package com.kaifamiao.month05.day15;


import java.util.*;

public class CountWord {

    public static  Map<Character,Integer> count(String str) {
        Map<Character,Integer> map = new HashMap<>();
        char[] chars = str.toCharArray();
        for (char curr : chars) {
            if (map.containsKey(curr)) { // 与这个等同，但是使用 containsKey 方法更简洁  map.keySet().contains(curr)
                map.put(curr, map.get(curr) + 1);
            } else {
                map.put(curr, 1);
            }
        }
        return map;
    }

    public static Map<Character,Integer> count2(String str) {
        Map<Character,Integer> map = new HashMap<>();
        int len = str.length();
        while (str.length() != 0) {
            char curr = str.charAt(0);
            str = str.replace(curr + "","");
            int value = len - str.length();
            map.put(curr,value);
            len = str.length();
        }
        return map;
    }

    public static void main(String[] args) {
        System.out.println("请输入一个字符串：");
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        Map<Character, Integer> map = count2(str);
        for (Map.Entry<Character,Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey() + " 字符出现的次数是： " + entry.getValue());
        }
    }
}
